package com.wmwj.hxlj.controller;

import com.wmwj.hxlj.entity.Article;
import com.wmwj.hxlj.entity.Comment;
import com.wmwj.hxlj.entity.User;
import com.wmwj.hxlj.entity.vo.*;
import com.wmwj.hxlj.service.ArticleService;
import com.wmwj.hxlj.service.CategoryService;
import com.wmwj.hxlj.service.CommentService;
import com.wmwj.hxlj.service.UserService;
import com.wmwj.hxlj.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private UserService userService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private CategoryService categoryService;

    // 新增方法：获取所有用户
    @GetMapping("/findUserList")
    public Result<PageResult<AdminUserVo>> getAllUsers(@RequestParam Integer pageStart) {
        return userService.getAllUsers(pageStart);
    }

    // 新增方法：批量删除用户
    @PostMapping("/batchDeleteUsers")
    public Result<String> batchDeleteUsers(@RequestBody List<Long> ids) {
        return userService.batchDeleteUsers(ids);
    }

    //id：获取用户信息
    @RequestMapping("/findById")
    public User findById(@RequestParam Long id){
        // 调用userService的findById方法，传入用户ID，并返回查询到的用户对象
        return userService.findById(id);
    }
    // 获取所有草稿文章，管理员可见
    @GetMapping("/drafts")
    public Result<List<Article>> getDraftArticles() {
        return articleService.getDraftArticles();
    }

    // 新增方法：获取所有文章
    @GetMapping("/getAllArticles")
    public Result<PageResult<Article>> adminGetAllArticles(@RequestParam Integer pageStart) {
        return articleService.adminGetAllArticles(pageStart);
    }

    //  管理员搜索文章文章
    @GetMapping("/searchArticles")
    public Result<PageResult<Article>> adminSearchArticles(@RequestParam String keyword, @RequestParam Integer pageStart) {
        return articleService.adminSearchArticles(keyword, pageStart);
    }

    //管理员通过id改变文章状态
    @GetMapping("/changeStatus")
    public Result<String> changeStatus(@RequestParam Long id) {
        return articleService.updateStatus(id);
    }

    // 获取所有评论
    @GetMapping("/getAll")
    public Result<PageResult<Comment>> getAllComments(@RequestParam int pageStart) {
        return commentService.getAllComments(pageStart);
    }

    // 管理员搜索评论
    @GetMapping("/searchComments")
    public Result<PageResult<Comment>> adminSearchComments(@RequestParam String keyword, @RequestParam Integer pageStart) {
        return commentService.adminSearchComments(keyword, pageStart);
    }

    //删除用户账户
    @DeleteMapping("/deleteUser")
    public Result<String> deleteUser(@RequestParam Long id) {
        return userService.deleteUser(id);
    }

    //通过账户或昵称搜索用户
    @GetMapping("/searchUsersByAccount")
    public Result<PageResult<AdminUserVo>> searchUsersByAccount(@RequestParam String account,@RequestParam Integer pageStart) {
        return userService.adminSearchUsersByAccount(account, pageStart);
    }

    //获取所有分类
    @GetMapping("/getAllCategory")
    public Result<PageResult<AdminCategoryVo>> adminGetAllCategory(@RequestParam Integer pageStart) {
        return categoryService.adminGetAllCategory(pageStart);
    }


}
